<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      :append-to-body="true"
      :title="title"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible"
      @closed="closeDialog"
    >
      <p slot="title">{{ title }}</p>
      <el-form
        ref="formData"
        :model="formData"
        :rules="formDataRules"
        label-width="120px"
        status-icon
      >
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="原序列号"
              prop="serialNum"
            >
              <span style="float: left;">{{ serialNum }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="修改序列号"
              prop="serialNum"
            >
              <el-input
                v-model="formData.serialNum"
                placeholder="请输入序列号"
                ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="dialog-footer" align="center">
        <el-button @click="reset()"
          >重置</el-button
        >
        <el-button type="primary" @click="onSubmit('formData')"
          >保存</el-button
        >
      </div>
      <el-divider content-position="left">修改日志</el-divider>
      <el-table
        v-if="tableList.length>0"
        ref="table"
        :data="tableList"
        height="auto"
        :header-cell-style="{ height: '50px' }"
        :cell-style="{ height: '50px'}"
        style="width: 100%;overflow: auto;"
      >
        <el-table-column label="修改人">
          <template slot-scope="scope">
            {{ scope.row.updateUserName }}
          </template>
        </el-table-column>
        <el-table-column label="修改时间">
          <template slot-scope="scope">
            {{ scope.row.updateTime }}
          </template>
        </el-table-column>
        <el-table-column label="修改前序列号">
          <template slot-scope="scope">
            {{ scope.row.beforeObject }}
          </template>
        </el-table-column>
        <el-table-column label="修改后序列号">
          <template slot-scope="scope">
            {{ scope.row.afterObject }}
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { UpdateSerialNum, getStorageChangeDataList } from "../../../api/Storage/StorageProductStore.js";
export default {
  name: "StorageProductStoreUpdateSerialNum",
  components: {
  },
  props: {
    dialogUpdateSerialNumVisible: {
      type: Boolean,
      default: false,
    },
    sourceId: {
      type: String,
      default: '',
    },
    serialNum: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      title: '修改序列号',
      loading: false,
      innerDialogFormVisible: false,
      formData: {
        serialNum: ''
      },
      formDataRules: {
        serialNum: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ]
      },
      tableList: [],
    };
  },
  watch: {
  },
  created() {
    this.innerDialogFormVisible = this.dialogUpdateSerialNumVisible;
    this.init();
  },
  methods: {
    async init() {
      if (this.sourceId) {
        await this.getDetail();
      }
    },
    getDetail() {
      getStorageChangeDataList({
        sourceIdType: 100,
        sourceId: this.sourceId
      }).then((res) => {
        this.tableList = res.data ? res.data : [];
      });
    },
    cancel() {
      this.innerDialogFormVisible = false;
      this.$emit("update:dialogUpdateSerialNumVisible", false);
      this.$emit("update:sourceId", "");
      this.$emit("update:serialNum", "");
    },
    closeDialog() {
      this.$parent.getList();
      this.cancel();
    },
    reset() {
      this.formData.serialNum = ''
    },
    onSubmit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true;
          UpdateSerialNum({
            id: this.sourceId,
            serialNum: this.formData.serialNum
          }).then((res) => {
            if (res.code === 200) {
              this.closeDialog();
            }
            this.loading = false;
          }).catch(() => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss">
.storage-product-store-dialog{
  .el-form{
    margin-top: 20px;
    overflow: hidden;
    .el-form-item{
      float: left;
    }
  }
  .base-content-operate{
    display: block;
    overflow: hidden;
    .el-button{
      float: left;
    }
  }
}
</style>
